package com.tm.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tm.ResultEntity;
import com.tm.mapper.SpLimitTimeShopMapper;
import com.tm.mapper.SpSkuInfoMapper;
import com.tm.mapper.SpSpellGroupMapper;
import com.tm.model.SpLimitTimeShop;
import com.tm.model.SpLimitTimeShopCriteria;
import com.tm.model.SpSkuInfo;
import com.tm.model.SpSpellGroup;
import com.tm.model.SpSpellGroupCriteria;
import com.tm.util.DateUtilsEx;

@Service
public class SpSkuInfoService {

	@Autowired
	private SpSkuInfoMapper ssiMapper;
	
	@Autowired
	private SpLimitTimeShopMapper sltsMapper;
	
	@Autowired
	private SpSpellGroupMapper ssgMapper;

	/**
	 * 分页获取直降活动商品列表
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, Object>> getSpLimitTimeShopByPage(int startIndex, int pageSize) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startIndex", startIndex);
		param.put("pageSize", pageSize);
		
		List<Map<String, Object>> list = sltsMapper.getSpLimitTimeShopByPage(param);
		return list;
	}

	/**
	 * 分页获取拼团活动商品列表
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public List<Map<String, Object>> getSpSpellGroupByPage(int startIndex, int pageSize) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startIndex", startIndex);
		param.put("pageSize", pageSize);
		
		List<Map<String, Object>> list = ssgMapper.getSpSpellGroupByPage(param);
		return list;
	}

	/**
	 * 获取首页小编推荐商品列表
	 * @return
	 */
	public List<Map<String, Object>> getRecommendSkuListForIndex(int startIndex, int pageSize) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startIndex", startIndex);
		param.put("pageSize", pageSize);
		return ssiMapper.getRecommendSkuListForIndex(param);
	}

	/**
	 * 根据分类id获取商品列表
	 * @param typeId
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public ResultEntity getSkuListByTypeId(Integer typeId, Integer startIndex, Integer pageSize, String orderType) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("startIndex", startIndex);
			param.put("pageSize", pageSize);
			param.put("typeId", typeId);
			param.put("orderType", orderType);
			
			List<Map<String, Object>> list = ssiMapper.getSkuListByTypeIdWithPage(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 获取商品详情
	 * @param skuId
	 * @return
	 */
	public SpSkuInfo getSpSkuInfoBySkuId(Integer skuId) {
		return ssiMapper.selectByPrimaryKey(skuId);
	}

	/**
	 * 获取商品团购信息
	 * @param skuId
	 * @return
	 */
	public SpSpellGroup getSpSpellGroupBySkuId(Integer skuId) {
		SpSpellGroupCriteria example = new SpSpellGroupCriteria();
		example.createCriteria().andSkuIdEqualTo(skuId).andIsDeleteEqualTo("N");
		List<SpSpellGroup> list = ssgMapper.selectByExample(example);
		if(list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * 获取商品直降信息
	 * @param skuId
	 * @return
	 */
	public SpLimitTimeShop getSpLimitTimeShopBySkuId(Integer skuId) {

		SpLimitTimeShopCriteria example = new SpLimitTimeShopCriteria();
		example.createCriteria().andSkuIdEqualTo(skuId).andIsDeleteEqualTo("N");
		List<SpLimitTimeShop> list = sltsMapper.selectByExample(example);
		if(list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * 获取商品banner展示图
	 * @param skuId
	 * @return
	 */
	public List<Map<String, Object>> getSkuPicBySkuId(Integer skuId) {
		return ssiMapper.getSkuPicBySkuId(skuId);
	}

	/**
	 * 获取商品标签属性
	 * @param skuId
	 * @return
	 */
	public List<Map<String, Object>> getSpSkuTagListBySkuId(Integer skuId) {
		return ssiMapper.getSpSkuTagListBySkuId(skuId);
	}

	/**
	 * 收藏商品（取消收藏）
	 * @param skuId
	 * @param memberId
	 * @return
	 */
	public ResultEntity addSpSkuCollect(Integer skuId, Integer memberId) {
		ResultEntity re = null;
		try{
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("skuId", skuId);
			param.put("memberId", memberId);
			
			//判断是否已收藏
			int rei = ssiMapper.countSpSkuCollect(param);
			
			if(rei > 0){
				//取消收藏
				rei = ssiMapper.deleteSpSkuCollect(param);
			}else{
				//新增收藏
				rei = ssiMapper.addSpSkuCollect(param);
			}
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "操作失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}
	
	
	/**
	 * 判断商品是否收藏
	 * @param skuId
	 * @param memberId
	 * @return
	 */
	public int countSpSkuCollect(Integer skuId, Integer memberId){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("skuId", skuId);
		param.put("memberId", memberId);
		
		//判断是否已收藏
		int rei = ssiMapper.countSpSkuCollect(param);
		
		return rei;
	}

	/**
	 * 获取会员购物车信息
	 * @param memberId
	 * @return
	 */
	public List<Map<String, Object>> getShopCartByMemberId(Integer memberId) {
		return ssiMapper.getShopCartByMemberId(memberId);
	}

	/**
	 * 删除购物车明细
	 * @param ids
	 * @return
	 */
	public ResultEntity delShopCartDetail(String ids) {
		ResultEntity re = null;
		try{
			
			String[] arr = ids.split(",");
			
			List<Integer> detailIds = new ArrayList<Integer>();

			for (String id : arr) {
				detailIds.add(Integer.parseInt(id));
			}
			
			int rei = ssiMapper.batchDelShopCartDetail(detailIds);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "删除失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 修改购物车明细
	 * @param id
	 * @param cnt
	 * @return
	 */
	public ResultEntity updateShopCartDetail(Integer id, Integer cnt) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", id);
			param.put("cnt", new BigDecimal(cnt));
			
			int rei = ssiMapper.updateShopCartDetail(param);
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "编辑失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 加入购物车
	 * @param skuId
	 * @param memberId
	 * @return
	 */
	public ResultEntity addShopCart(Integer skuId, Integer memberId, String skuNo, String skuUnit, String skuPrice, String skuAmount) {
		ResultEntity re = null;
		try{
			
			//判断会员购物车是否存在，若不存在则新增一条购物车记录
			Map<String, Object> cart = ssiMapper.getCardByMemberId(memberId);
			
			if(cart == null){
				cart = new HashMap<String, Object>();
				cart.put("memberId", memberId);
				cart.put("createTime", DateUtilsEx.formatToString(new Date(), DateUtilsEx.DATE_FORMAT_SEC));
				
				ssiMapper.addMemberShopCart(cart);
			}
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("shoppingCartId", cart.get("shopping_cart_id"));
			param.put("skuId", skuId);
			param.put("memberId", memberId);
			param.put("skuNo", skuNo);
			param.put("skuUnit", skuUnit);
			param.put("skuPrice", skuPrice);
			param.put("skuAmount", skuAmount);
			
			//判断是否已加入过购物车
			int rei = ssiMapper.countSkuShopCart(param);
			
			if(rei > 0){
				//修改数量加
				rei = ssiMapper.updatekuShopCart(param);
			}else{
				//新增一条明细记录
				rei = ssiMapper.addkuShopCart(param);
			}
			
			if(rei > 0){
				re = new ResultEntity("0");
			}else{
				re = new ResultEntity("-1", "加入购物车失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 获取会员当前购物车商品数量
	 * @param memberId
	 * @return
	 */
	public int getGwcCntByMemberId(Integer memberId) {
		return ssiMapper.getGwcCntByMemberId(memberId);
	}

	/**
	 * 根据购物车详情明细id获取从购物车跳转结算信息
	 * @param ids
	 * @return
	 */
	public List<Map<String, Object>> getShopCartPaySkuInfo(String ids) {
		String[] arr = ids.split(",");
		
		List<Integer> detailIds = new ArrayList<Integer>();

		for (String id : arr) {
			detailIds.add(Integer.parseInt(id));
		}
		
		return ssiMapper.getShopCartPaySkuInfo(detailIds);
	}

	/**
	 * 拼团
	 * @param skuId
	 * @param memberId
	 * @param money
	 * @return
	 */
	public ResultEntity addSpSpellGroupParticipate(Integer skuId, Integer memberId, String money, Integer spellId) {
		ResultEntity re = null;
		try{
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("skuId", skuId);
			param.put("memberId", memberId);
			param.put("money", money);
			param.put("spellId", spellId);
			param.put("id", 0);
			
			List<Map<String, Object>> list = ssiMapper.getSpSpellGroupParticipate(param);
			
			if(list.size() > 0){
				Map<String, Object> rem = list.get(0);
				if("Y".equals(rem.get("pay_state"))){
					re = new ResultEntity("-1", "您已参与过该拼团！");
					return re;
				}else{
					re = new ResultEntity("0", null, rem.get("participate_id"));
					return re;
				}
			}
			
			int rei = ssiMapper.addSpSpellGroupParticipate(param);
			
			if(rei > 0){
				re = new ResultEntity("0", null, param.get("id"));
			}else{
				re = new ResultEntity("-1", "操作失败！");
			}
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 获取会员收藏商品列表
	 * @param memberId
	 * @param startIndex
	 * @param pageSize
	 * @return
	 */
	public ResultEntity getCollectListByMemberId(Integer memberId, Integer startIndex, Integer pageSize) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("memberId", memberId);
			param.put("startIndex", startIndex);
			param.put("pageSize", pageSize);
			
			List<Map<String, Object>> list = ssiMapper.getCollectListByMemberIdWithPage(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		
		return re;
	}

	/**
	 * 根据关键字获取商品列表
	 * @param keyword
	 * @param startIndex
	 * @param pageSize
	 * @param orderType
	 * @return
	 */
	public ResultEntity getSkuListByKeyword(String keyword, Integer startIndex, Integer pageSize, String orderType) {
		ResultEntity re = null;
		try{
			
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("startIndex", startIndex);
			param.put("pageSize", pageSize);
			param.put("keyword", "%" + keyword + "%");
			param.put("orderType", orderType);
			
			List<Map<String, Object>> list = ssiMapper.getSkuListByKeywordWithPage(param);
			
			re = new ResultEntity(list);
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

	/**
	 * 修改直降、拼团活动状态
	 * @return
	 */
	public ResultEntity updateLimitAndGroupState() {
		ResultEntity re = null;
		try{
			
			//修改直降商品为开始状态
			sltsMapper.updateSpellStatus1();
			
			//修改直降商品为结束状态
			sltsMapper.updateSpellStatus3();
			
			//修改拼团活动为开始状态
			ssgMapper.updateSpellStatus1();
			
			//修改拼团活动为成功状态
			ssgMapper.updateSpellStatus2();
			
			//修改拼团活动为结束状态
			ssgMapper.updateSpellStatus4();
			
			re = new ResultEntity("0");
			
		}catch(Exception e){
			e.printStackTrace();
			re = new ResultEntity("-1", "系统异常！");
		}
		return re;
	}

}
